initFollowPrompts();

/**
* jQuery.HTML5 placeholder - Placeholder plugin for input fields
* Written by Ludo Helder (ludo DOT helder AT gmail DOT com)
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
* Date: 2010/11/18
*
* @author Ludo Helder
* @version 1.0.1
*
**/

/* Drop-down menus on hover */
$('#nav li').hover(
    function () { $(this).find('ul.tabs').show(); },
    function () { $(this).find('ul.tabs').hide(); }
);

/*
* jQuery extensions
*/

// Shows the current element(s) and hides the element(s) passed or specified by selector.
jQuery.fn.showAndHide = function (elemToHide) {
    $(elemToHide).hide();
    return this.show();
};

// Returns model id parsed from element id w/ format "prefix-<id>"
jQuery.fn.modelId = function () {
    var id = $(this).attr('id');
    if (id == null) return null; // Just in case; browsers tested return empty string for missing id.

    var idParts = id.split(/[-_]/g); // Split on hyphens and underscores
    return (idParts.length > 1) ? idParts[idParts.length - 1] : null;
};

/*
* Custom libs
*/

Notify = {
    success: function (message) {
        Notify.notify('success', message);
    },
    error: function (message) {
        Notify.notify('error', message);
    },
    notify: function (typeOfNoticeClass, message) {
        $('.notice').hide();
        $('#ajax-message').text(message);
        $('.notice').
            removeClass().
            addClass('notice').
            addClass(typeOfNoticeClass).
            show();
    }
};

ShowAndHideControl = function (options) {
    var elemToShowAndHide = $(options['target']);
    var showTriggers = $(options['showControl']);
    var hideTriggers = $(options['hideControl']);

    showTriggers.
        click(function () {
            elemToShowAndHide.showAndHide(showTriggers);
            return false;
        });

    hideTriggers.
        click(function () {
            elemToShowAndHide.hide();
            showTriggers.show();
            return false;
        });
};

(function ($) {
    $.fn.fadeable = function (options) {

        var settings = {
            target: this,
            duration: 'fast'
        };
        $.extend(settings, options);

        $(this).live('hover', function (event) {
            switch (event.type) {
                case 'mouseenter':
                    $(this).find(settings.target).stop().fadeTo(settings.duration, 1);
                    break;
                case 'mouseleave':
                    $(this).find(settings.target).stop().fadeTo(settings.duration, 0);
                    break;
            }
        });
    };
})(jQuery);

// show screenshot info on hover
$('ol.dribbbles li div.dribbble-img').fadeable({ target: 'a.dribbble-over' });

// show prev/next arrows on hover
$('ol.prevnext li a').fadeable({ target: 'strong' });

// show zoom icon on thumbnail hover
$('ol.activity li div.act-shot').fadeable({ target: 'strong' });
$('div.the-rebound div.dribbble-img').fadeable({ target: 'a.dribbble-over' });

// show zoom icon on multi thumb hover
$('ol.multi-grid li a').fadeable({ target: 'strong' });

$('div.overlay').each(function () {
    var $overlay = $(this);
    $overlay.find('a.close').live('click', function () {
        $overlay.hide();
        return false;
    });
});

// show/hide pixel useage help
$('#pixels-help-a').click(function () {
    $('#pixels-help').slideToggle("normal");
    return false;
});

$('[rel=tipsy]').tipsy({ fade: true, gravity: 's' });

/*
* Attaches to behavior to any follow/unfollow link on the page.
*/

function initFollowPrompts() {
    $('.follow-prompt a').live('click', function () {
        var $link = $(this);
        var followPrompt = $link.closest('.follow-prompt');
        var originalHtml = followPrompt.html();

        $link.closest('.follow-prompt form').ajaxSubmit({
            beforeSend: function () {
                $link.addClass('processing');
                $link.find('span').text('Wait...');
            },
            success: function (responseHtml) {
                // Update to refect follow status
                $link.closest('.follow-prompt').html(responseHtml);
            },
            error: function (request) {
                followPrompt.html(originalHtml);
                alert(request.responseText);
            }
        });
        return false;
    });
}